package com.KegController.BrewCoder;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.os.Parcel;
import android.os.Parcelable;

public class android2Arduino implements Parcelable{
	private boolean androidOverride;
	private boolean compressorON;
	private boolean heaterON;
	private boolean lightON;
	private boolean celsius;

	boolean[] bitArray = new boolean[]{androidOverride,compressorON,heaterON,celsius};

	private long currentInteriorTemp;
	private int SetTempIndex;

	private String[] tempFlist;
	private String[] tempClist;
	
	private int MAXTEMPLIST = 100;
	
	NumberFormat formatter = new DecimalFormat("##.##");
	
	public android2Arduino(){
		this.androidOverride = false;
		this.compressorON = false;
		this.heaterON = false;
		this.celsius = false;
		this.currentInteriorTemp = -1;
		this.createTempList();
		this.SetTempIndex = 17; //41F
	}

	public android2Arduino(Parcel in) {
		readFromParcel(in);
	}
	
	public boolean isAndroidOverride() {
		return androidOverride;
	}

	public void setAndroidOverride(boolean androidOverride) {
		this.androidOverride = androidOverride;
	}

	public boolean isCompressorON() {
		return compressorON;
	}

	public void setCompressorON(boolean compressorON) {
		this.compressorON = compressorON;
	}

	public boolean isLightON() {
		return lightON;
	}

	public void setLightON(boolean lightON) {
		this.lightON = lightON;
	}

	public boolean isHeaterON() {
		return heaterON;
	}

	public void setHeaterON(boolean heaterON) {
		this.heaterON = heaterON;
	}

	public void setDisplayMode(boolean celsius){
		this.celsius = celsius;
	}
	
	public void setCurrentInteriorTemp(long currentInteriorTemp){
		this.currentInteriorTemp = currentInteriorTemp;
	}
	
	public long getCurrentInteriorTemp(){
		return currentInteriorTemp;
	}
	
	public String getCurrentInteriorTempFormatted(){
		if(currentInteriorTemp == -1)
			return "No Data";
		if(celsius)
		return formatter.format(currentInteriorTemp* .01);
		else
			return formatter.format(convertCtoF(currentInteriorTemp)* .01);
	}
	
	public String[] getDialogArryList(){
		if(celsius)
			return tempClist;
		else
			return tempFlist;		
	}
	
	public int getSetTempIndex() {
		return SetTempIndex;
	}

	public void setSetTemptIndex(int SetTempIndex) {
		this.SetTempIndex = SetTempIndex;
		}

	public int getSetTemp(){
			return (int) (Double.parseDouble(tempClist[SetTempIndex])* 100);

	}

	public String getSetTempFormated(){
		
		if(celsius)
			return tempClist[SetTempIndex];
		else
			return tempFlist[SetTempIndex];
	}
	
	private double convertCtoF(double tmp){
		return (tmp*1.8) + 32;
	}
	
	private double convertFtoC(double tmp){
		return (tmp-32) * (.5555555);
	}
	
	private void createTempList(){
		double count = 0;
		tempClist = new String[MAXTEMPLIST+1];
		tempFlist = new String[MAXTEMPLIST+1];
	    for(int x = 0; x < MAXTEMPLIST; x++){
	    	tempClist[x] = String.valueOf(count += .5);
	    }
	    for(int y = 0; y < MAXTEMPLIST; y++){
	    	tempFlist[y] = String.valueOf(convertCtoF(Double.parseDouble(tempClist[y])));
	
	    }
	}

//	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

//	@Override
	public void writeToParcel(Parcel dest, int arg1) {
		// TODO Auto-generated method stub
		dest.writeLong(currentInteriorTemp);
		dest.writeInt(SetTempIndex);
//		dest.writeStringArray(tempClist);
//		dest.writeStringArray(tempFlist);
		dest.writeInt(MAXTEMPLIST);
		dest.writeBooleanArray(bitArray);
	}
	private void readFromParcel(Parcel in) {
		 
		// We just need to read back each
		// field in the order that it was
		// written to the parcel
		currentInteriorTemp = in.readLong();
		SetTempIndex = in.readInt();
		MAXTEMPLIST = in.readInt();
		androidOverride = bitArray[0];
		compressorON = bitArray[1];
		heaterON = bitArray[2];
		celsius = bitArray[3];
	}
 
    /**
     *
     * This field is needed for Android to be able to
     * create new objects, individually or as arrays.
     *
     * This also means that you can use use the default
     * constructor to create the object and use another
     * method to hyrdate it as necessary.
     *
     * I just find it easier to use the constructor.
     * It makes sense for the way my brain thinks ;-)
     *
     */
    public static final Parcelable.Creator CREATOR =
    	new Parcelable.Creator() {
            public android2Arduino createFromParcel(Parcel in) {
                return new android2Arduino(in);
            }
 
            public android2Arduino[] newArray(int size) {
                return new android2Arduino[size];
            }
        };
}
